<template>
  <div class='container'>
    <el-dialog
      :title="diaTitle"
      :visible="dialogVisible"
      width="60%"
      @close="cancelAdd"
    >
      <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="100px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="addForm.title" placeholder="请输入文章标题"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <quill-editor style="height: 200px" :options="quillOption" v-model="addForm.articleBody">
          </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" style="margin-top: 70px">
          <el-input v-model="addForm.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="cancelAdd">取 消</el-button>
          <el-button type="primary" @click="btnOk">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, detail, update } from '@/api/hmmm/articles'
export default {
  components: {
    quillEditor
  },
  props: {
    dialogVisible: Boolean,
    diaTitle: String
  },
  methods: {
    async getArticleDetail (id) {
      const { data } = await detail({ id })
      this.addForm = data
    },
    async btnOk () {
      try {
        if (this.diaTitle === '添加技巧') {
          await this.$refs.addForm.validate()
          await add(this.addForm)
          this.$message.success('添加成功')
        } else {
          await this.$refs.addForm.validate()
          await update(this.addForm)
          this.$message.success('修改成功')
        }
        this.$emit('getlist')
        this.cancelAdd()
      } catch (e) {
        console.log(e)
      }
    },
    cancelAdd () {
      this.$refs.addForm.resetFields()
      this.addForm = {
        title: null,
        articleBody: null,
        videoURL: null
      }
      this.$emit('update:dialogVisible', false)
    }
  },
  data () {
    return {
      quillOption: {
        modules: {
          toolbar: {
            container: [
              ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
              ['blockquote', 'code-block'], // 引用，代码块
              [{ list: 'ordered' }, { list: 'bullet' }], // 有序列表，无序列表
              ['link', 'image', 'video']
            ]
          }
        }
      },
      addRules: {
        title: [{
          required: true,
          message: '请输入文章标题',
          trigger: 'blur'
        }],
        articleBody: [{
          required: true,
          message: '请输入文章内容',
          trigger: 'blur'
        }]
      },
      addForm: {
        title: null,
        articleBody: null,
        videoURL: null
      }
    }
  }
}
</script>

<style scoped lang='less'>
.ql-editor .ql-blank{
  height: 200px;
}
</style>
